Dictionary as a MAP type¶
- course_professors = {‘calculus’: ‘Prof. Kotter’,
‘diff eq’: ‘Prof. Feeny’, ‘linear algebra’: ‘Prof. Kotter’, ‘real analysis’: ‘Prof. Crabtree’}
- student_courses = {‘vinnie’: {‘calculus’, ‘diff eq’},
‘arnold’: {‘calculus’, ‘linear algebra’}, ‘juan luis’: {‘real analysis’}}
Which student has which professor?¶
student_professors = {}
for student in student_courses:
for course in student_courses[student]:
if student not in student_professors:
# use set to remove duplicates
student_professors[student] = set()
student_professors[student].add(course_professors[course])
student_professors
Output:
{'vinnie': {'Prof. Feeny', 'Prof. Kotter'},
'arnold': {'Prof. Kotter'},
'juan luis': {'Prof. Crabtree'}}
Which professor has which student?¶
professor_students = dict()
for course, professor in course_professors.items():
for student, courses in student_courses.items():
if course in courses:
if professor not in professor_students:
# use set to remove duplicates
professor_students[professor] = set()
professor_students[professor].add(student)
professor_students
Output:
{'Prof. Kotter': {'arnold', 'vinnie'},
'Prof. Feeny': {'vinnie'},
'Prof. Crabtree': {'juan luis'}}